package com.chuanjiabao.wisdom.util.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.bean.UpdateFileTeam;
import com.chuanjiabao.wisdom.databinding.DialogJoinQueueBinding;
import com.chuanjiabao.wisdom.greendao.gen.UpdateFileTeamDao;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.chuanjiabao.wisdom.listener.OnDataCompleteListener;
import com.chuanjiabao.wisdom.ui.activity.UpdateFileActivity;
import com.chuanjiabao.wisdom.util.BHThreadPool;
import com.chuanjiabao.wisdom.util.GlideUtil;

import java.util.List;

import static com.chuanjiabao.wisdom.bean.Constants.REQEEST_CDOE_GPS;
import static com.chuanjiabao.wisdom.bean.Constants.REQUEST_CODE;

/**
 * 加入队列
 */
public class JoinQueueDialog extends BaseDialog implements View.OnClickListener {

    private Context context;
    private DialogJoinQueueBinding binding;
    private OnBHClickListener onBHClickListener;
    private OnDataCompleteListener listener;
    private boolean isRunning = true;
    private int checkCount = 0;
    private int type = -1;

    public void setOnBHClickListener(OnBHClickListener onBHClickListener) {
        this.onBHClickListener = onBHClickListener;
    }

    public void setOnCompleteListener(int type, OnDataCompleteListener listener) {
        this.type = type;
        this.listener = listener;
    }

    public JoinQueueDialog(Context context) {
        super(context);
        this.context = context;
        binding = DataBindingUtil.inflate(
                LayoutInflater.from(context),
                R.layout.dialog_join_queue,
                null,
                false
        );
        binding.setDialog(this);
        setContentView(binding.getRoot());
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(layoutParams);
        //弹窗弹出在底部，默认中部
        getWindow().setGravity(Gravity.BOTTOM);
        initView();
    }

    private void initView() {
        binding.dialogNo.setOnClickListener(this);
        binding.tvModuleUpdateAgain.setOnClickListener(this);
        binding.llModuleTeam.setOnClickListener(this);
        setOnDismissListener(dialog -> isRunning = false);

        UpdateFileTeamDao teamDao =
                FApplication.getInstance()
                        .getDaoSession()
                        .getUpdateFileTeamDao();

        //开启子线程获取图片数量
        @SuppressLint("SetTextI18n")
        Runnable runnable = () -> {
            while (isRunning) {
                List<UpdateFileTeam> listSecond = teamDao.loadAll();
                if (!listSecond.isEmpty()) {
                    Message msg = new Message();
                    msg.what = REQUEST_CODE;
                    msg.arg1 = listSecond.size();
                    msg.obj = listSecond.get(0).getFileName();
                    handler.sendMessage(msg);
                } else {
                    handler.sendEmptyMessage(REQEEST_CDOE_GPS);
                }
                SystemClock.sleep(1000);
            }
        };

        BHThreadPool.getInstance().executeTask(runnable);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REQUEST_CODE:
                    binding.tvModuleNum.setText(msg.arg1 + "个文件加入队列");
                    String newUrl = (String) msg.obj;
                    String oldUrl = (String) binding.ivModule.getTag(R.id.image_tag);
                    if (!TextUtils.equals(oldUrl, newUrl)) {
                        binding.ivModule.setVisibility(View.VISIBLE);
                        GlideUtil.setRoundUriMethod(getActivity(), newUrl, binding.ivModule, 5);
                        binding.ivModule.setTag(R.id.image_tag, newUrl);
                    }
                    checkCount = 3;
                    break;
                case REQEEST_CDOE_GPS:
                    binding.tvModuleNum.setText("上传文件准备中");
                    binding.ivModule.setVisibility(View.GONE);
                    binding.ivModule.setTag(R.id.image_tag, null);

                    checkCount++;
                    if (checkCount > 2 && isShowing()) {
                        if (listener != null) listener.onNext(type);
                        dismiss();
                    }
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llModuleTeam:
                context.startActivity(new Intent(context, UpdateFileActivity.class));
                break;
            case R.id.dialogNo:
                if (onBHClickListener != null) {
                    onBHClickListener.onCloseClick();
                }
                dismiss();
                break;
            case R.id.tvModuleUpdateAgain:
                if (onBHClickListener != null) {
                    onBHClickListener.onConfirmClick();
                }
                dismiss();
                break;
        }
    }

}


